package com.approval.approvalapp.index.dbsx.qjsp;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approval.QjSplcAdapter;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.QjxqBean;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.DownResponseInterface;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class QjspYbXqActivity extends BaseActivity {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.name_tv)
    TextView nameTv;
    @Bind(R.id.civ)
    CircleImageView civ;
    @Bind(R.id.colortv)
    TextView colortv;
    @Bind(R.id.qjfl_tv)
    TextView qjflTv;
    @Bind(R.id.qjlx_tv)
    TextView qjlxTv;
    @Bind(R.id.kssj_tv)
    TextView kssjTv;
    @Bind(R.id.jssj_tv)
    TextView jssjTv;
    @Bind(R.id.qjts_tv)
    TextView qjtsTv;
    @Bind(R.id.qjsm_tv)
    TextView qjsmTv;
    @Bind(R.id.tv)
    TextView tv;
    @Bind(R.id.refuse_tv)
    TextView refuseTv;
    @Bind(R.id.agree_tv)
    TextView agreeTv;
    @Bind(R.id.rv)
    RecyclerView rv;
    @Override
    public int getLayoutId() {
        return R.layout.activity_qjsp_yb_xq;
    }

    @Override
    public void initView() {
        String id = getIntent().getStringExtra("id");
        String qjlx = getIntent().getStringExtra("qjlx");
        String state = getIntent().getStringExtra("state");
        if (state.equals("✓ 审核通过") ) {
            colortv.setTextColor(Color.parseColor("#1465FD"));
            colortv.setText("审核通过");

            ColorDrawable colorDrawable = new ColorDrawable(
                    getResources().getColor(R.color.colorAccent));

            civ.setImageDrawable(colorDrawable);
        } else if (state.equals("✓ 初审通过") ){
            colortv.setTextColor(Color.parseColor("#1465FD"));
            colortv.setText("初审通过");

            ColorDrawable colorDrawable = new ColorDrawable(
                    getResources().getColor(R.color.colorAccent));

            civ.setImageDrawable(colorDrawable);
        } else {
            colortv.setTextColor(Color.parseColor("#DE463A"));
            colortv.setText("审核不通过");
            ColorDrawable colorDrawable = new ColorDrawable(
                    getResources().getColor(R.color.red));

            civ.setImageDrawable(colorDrawable);
        }
        qjlxTv.setText(qjlx);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("leave_id", id);

        NetRequsetUtil.getInstance().netRequestPostJson("v1/Center/leave_show", jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                final QjxqBean qjxqBean = JSON.parseObject(str, QjxqBean.class);
                if (qjflTv == null){
                    return;
                }
                rv.setLayoutManager(new LinearLayoutManager(QjspYbXqActivity.this));
                QjSplcAdapter qjSplcAdapter = new QjSplcAdapter();
                qjSplcAdapter.setListBeanList(qjxqBean.getList());
                rv.setAdapter(qjSplcAdapter);

                qjflTv.setText(qjxqBean.getFen_lei_type());
                kssjTv.setText(qjxqBean.getStart());
                jssjTv.setText(qjxqBean.getEnd());
                qjtsTv.setText(qjxqBean.getDay() + "天");
                qjsmTv.setText(qjxqBean.getContent());
                nameTv.setText(qjxqBean.getName());
                titleTv1.setText(qjxqBean.getName() + "的请假申请");
                if (TextUtils.isEmpty(qjxqBean.getFile_url())) {
                    tv.setVisibility(View.GONE);
                } else {
                    tv.setVisibility(View.VISIBLE);
                    tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final ProgressDialog progressDialog = new ProgressDialog(QjspYbXqActivity.this);
                            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                            progressDialog.setProgressNumberFormat(" ");
                            progressDialog.setTitle("正在下载");
                            progressDialog.setMessage("请稍候...");
                            progressDialog.setProgress(0);
                            progressDialog.setMax(100);
                            progressDialog.setCanceledOnTouchOutside(false);
                            progressDialog.show();
                            Log.d("QjspYbXqActivity11", qjxqBean.getFile_url());
                            Log.d("QjspYbXqActivity11", qjxqBean.getFile_url().
                                    substring((qjxqBean.getFile_url().lastIndexOf("/") + 1),
                                            qjxqBean.getFile_url().length()));
                            NetRequsetUtil.getInstance().downLoadFile(QjspYbXqActivity.this,
                                    qjxqBean.getFile_url(),
                                    qjxqBean.getFile_url().
                                            substring((qjxqBean.getFile_url().lastIndexOf("/") + 1),
                                                    qjxqBean.getFile_url().length())
                                    , new DownResponseInterface() {
                                        @Override
                                        public void successFinish() {
                                            progressDialog.dismiss();

                                        }

                                        @Override
                                        public void progress(int progress) {
                                            progressDialog.setProgress(progress);

                                        }

                                        @Override
                                        public void erro() {
                                            progressDialog.dismiss();

                                        }
                                    });
                        }
                    });
                }

            }

            @Override
            public void errowithresponse(String str) {

            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {

            }
        });

    }

    @Override
    public void initData() {

    }


    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }
}
